<template>
  <div class="table-page-search-wrapper">
    <a-card>
      <a-form layout="inline">
        <a-row :gutter="48">
          <a-col :md="6" :sm="12">
            <a-form-item label="所属单位">
              <a-select :disabled="selectMenu.yx.length === 1" allowClear v-model="queryParam.kcssyxm" showSearch :filterOption="filterOption">
                <a-select-option v-for="i in selectMenu.yx" :key="i.id" :value="i.dwh">{{ i.dwh + ' ' + i.dwmc }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="12">
            <a-form-item label="课程代码">
              <a-input v-model="queryParam.kch"/>
            </a-form-item>
          </a-col>
          <template v-if="advanced">
            <a-col :md="6" :sm="12">
              <a-form-item label="课程名称">
                <a-input v-model="queryParam.kcmc"/>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="12">
              <a-form-item label="课程状态">
                <a-select allowClear v-model="queryParam.kczt">
                  <a-select-option v-for="i in TYPE0338" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>

            <a-col :md="6" :sm="12">
              <a-form-item
                label="审核时间"
              >
                <a-range-picker v-model="queryParam.auditTime"/>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="12">
              <a-form-item label="开课状态">
                <a-select allowClear v-model="queryParam.kkzt">
                  <a-select-option value="01">启用</a-select-option>
                  <a-select-option value="02">停用</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
          </template>
          <a-col :md="16" :sm="20">
            <span class="table-page-search-submitButtons" >
            <a-button type="primary" @click="$refs.table.refresh(true)">查询</a-button>
            <a-button type="primary" style="margin:0 10px" @click="exportExcel">导出</a-button>
            <a-button type="primary" @click="toAdd">新建课程</a-button>
            <a-button @click="queryParam = {};$refs.table.refresh()" style="margin-left: 8px;">重置</a-button>
            <a @click="advanced = !advanced" style="margin-left: 8px">
                {{ advanced ? '收起' : '展开' }}
                <a-icon :type="advanced ? 'up' : 'down'"/>
              </a>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </a-card>

    <a-card :bordered="false" style="margin-top: 10px">
      <s-table
        v-if="initDone"
        ref="table"
        size="default"
        rowKey="id"
        :columns="columns"
        :data="loadData"
        :alert="{ show: true }"
        :pageSize="parameter.pageSize"
        :pageNum="parameter.pageNum"
        :scroll="{ x: 1600 }"
      >
        <span slot="auditTime" slot-scope="a">{{a && a.slice(0,11)}}</span>
        <span slot="serial" slot-scope="text, record, index">
          {{ index + 1 + (parameter.pageNo - 1) * parameter.pageSize }}
        </span>
        <template slot="operation" slot-scope="a,b">
          <a @click="showDetail(b)">查看明细</a>
          <a-divider type="vertical"/>
          <a-dropdown>
            <a class="ant-dropdown-link">
              更多 <a-icon type="down"/>
            </a>
            <a-menu slot="overlay">
              <a-menu-item>
                <a @click="toEdit(b)">修改</a>
              </a-menu-item>
              <a-menu-item>
                <a @click="deleteCourse(b)">删除</a>
              </a-menu-item>
            </a-menu>
          </a-dropdown>
        </template>
      </s-table>
    </a-card>
    <course-detail :confirmLoading="confirmLoading" :courseInfo="courseInfo" :visible="detailVisible"
                   @cancel="detailVisible = false"/>
  </div>
</template>

<script>
  import {STable} from '~'
  import {mapState} from 'vuex'
  import courseDetail from '../courseDetail'
  import searchMixin from '@/utils/searchRemain'

  export default {
    mixins: [searchMixin],
    data() {
      return {
        advanced: true,
        queryParam: {},
        columns: [
          {
            title: '序号',
            scopedSlots: {customRender: 'serial'}
          },
        {
          title: '课程代码',
          dataIndex: 'kch'
        },
        {
          title: '课程名',
          dataIndex: 'kcmc'
        },
        {
          title: '英文名',
          dataIndex: 'kcywmc'
        },
        {
          title: '所属单位',
          dataIndex: 'ssyxmc'
        },
        {
          title: '课程层次',
          dataIndex: 'kcccmc'
        },
        {
          title: '课程状态',
          dataIndex: 'kcxxztmc'
        },
        {
          title: '开课状态',
          dataIndex: 'kkztc'
        },
        {
          title: '审核时间',
          dataIndex: 'auditDate',
          scopedSlots: {customRender: 'auditTime'}
        },

        {
          title: '学分',
          dataIndex: 'xf'
        },
        {
          title: '操作',
          width: '160px',
          dataIndex: 'operation',
          fixed: 'right',
          scopedSlots: { customRender: 'operation' }
        }
      ],
      parameter: {
        pageNo: 1,
        pageSize: 10
      },
      loadData: parameter => {
        this.parameter = parameter
        parameter.pageNum = parameter.pageNo
        let params = Object.assign(this.queryParam, parameter)
        if (this.queryParam.auditTime) {
          params = {
            ...params,
            auditTimeStartDate: params.auditTime && params.auditTime[0].format('YYYY-MM-DD') + ' 00:00:00',
            auditTimeEndDate: params.auditTime && params.auditTime[1].format('YYYY-MM-DD') + ' 23:59:59'
          }
        }
        return this.$api.cultivate.courseList(params).then(res => {
          return {
            data: res.data.rows,
            pageNo: parameter.pageNum,
            pageSize: parameter.pageSize,
            totalPage: Math.ceil(res.data.total / parameter.pageSize),
            totalCount: res.data.total
          }
        })
      },
      selectMenu: {
        yx: []
      },
      detailVisible: false,
      confirmLoading: false,
      courseInfo: {},
      initDone: false
    }
  },
  components: {STable, courseDetail},
  computed: {
    ...mapState({
      TYPE0338: state => state.app['TYPE0338'] // 课程状态
    })
  },
  created () {
    this.$store.dispatch('app/setDictionary', 'TYPE0338')
  },
  methods: {
    filterOption (value, op) {
      return op.componentOptions.children[0].text.indexOf(value) !== -1
    },
    initSelectMenu () {
      let allRequest = [
        this.$api.base.collegeAll()
      ]
      Promise.all(allRequest).then(res => {
        this.selectMenu = {
          yx: res[0].data
        }
        if(this.selectMenu.yx && this.selectMenu.yx.length == 1){
          this.$set(this.queryParam, 'kcssyxm', this.selectMenu.yx[0].dwh)
        }
        this.initDone = true
      })
    },
    deleteCourse (c) {
      this.$confirm({
        title: '确定删除所选课程信息?',
        content: '当您点击确定按钮后，此课程信息将会被彻底删除',
        centered: true,
        onOk: () => {
          this.$api.cultivate.courseDelete(c.id).then(() => {
            this.$message.success('删除成功')
            this.selectedRowKeys = []
            this.selectedRows = []
            this.$refs.table.refresh(false)
            this.$refs.table.clearSelected()
          })
        },
        onCancel: () => { }
      })
    },
    showDetail (course) {
      this.confirmLoading = true
      this.$api.cultivate.courseDetail(course.id).then(res => {
        this.confirmLoading = false
        this.courseInfo = res.data
      })
      this.detailVisible = true
    },
    toAdd () {
      this.$router.push('/cultivate/course/add')
    },
    toEdit (course) {
      this.$router.push(`/cultivate/course/add?id=${course.id}`)
    },
    filterOption (value, op) {
      return op.componentOptions.children[0].text.indexOf(value) !== -1
    },
    exportExcel(){
      let params = {...this.queryParam}
      delete params.pageNo
      delete params.pageNum
      delete params.pageSize
      this.$export('foster/kcxx/exportList', params)
    }
  }
}
</script>

<style>

</style>
